package com.jianhua.appmarket.module.service.system.impl;

import com.jianhua.appmarket.common.util.SnowflakeKeyUtil;
import com.jianhua.appmarket.module.dao.system.DictionaryDao;
import com.jianhua.appmarket.module.entity.system.Dictionary;
import com.jianhua.appmarket.module.model.system.UserVO;
import com.jianhua.appmarket.module.service.system.IDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DictionaryService implements IDictionaryService {

    @Autowired
    private DictionaryDao dictionaryDao;

    @Override
    public List<Dictionary> listAllByTypeId(Long typeId) {
        Dictionary param = new Dictionary();
        param.setTypeId(typeId);
        return dictionaryDao.listByParam(param);
    }

    @Override
    public Boolean saveOrEditDic(Dictionary dictionary, UserVO user) {
        if(Objects.isNull(dictionary.getId())){
            dictionary.setId(SnowflakeKeyUtil.generateId());
            dictionary.setCreateUser(user.getNickName());
            dictionary.setUpdateUser(user.getNickName());
            return dictionaryDao.insert(dictionary) > 0;
        }else {
            return editDic(dictionary,user);
        }
    }

    @Override
    public Boolean editDic(Dictionary dictionary, UserVO user) {
        dictionary.setUpdateUser(user.getNickName());
        return dictionaryDao.update(dictionary) > 0;
    }

    @Override
    public Boolean deleteDic(Long dicId) {
        return dictionaryDao.deleteById(dicId) > 0;
    }
}
